package com.cali.libcore.dialog

import android.content.Context
import android.text.Html
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.ViewTreeObserver
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import com.cali.libcore.R
import com.cali.libcore.dialog.manager.ScreenUtil

import java.lang.ref.WeakReference

/**
 * Created by mq on 2018/9/1 上午10:58
 * mqcoder90@gmail.com
 */

class MyDialogController internal constructor(dialog: IDialog) {

    internal var layoutRes: Int = 0
    internal var dialogWidth: Int = 0
        private set
    internal var dialogHeight: Int = 0
        private set
    internal var dimAmount = 0.2f
        private set
    var gravity = Gravity.CENTER
        private set
    internal var isCancelableOutside = true
        private set
    internal var isCancelable: Boolean = false
        private set
    internal var animRes: Int = 0
        private set
    internal var dialogView: View? = null
    private var mPositiveButtonListener: IDialog.OnClickListener? = null
    private var mNegativeButtonListener: IDialog.OnClickListener? = null
    private var mDialog: WeakReference<IDialog> = WeakReference(dialog)
    private var titleStr: String? = null//默认标题
    private var contentStr: String? = null//默认内容
    private var positiveStr: String? = null//右边按钮文字
    private var negativeStr: String? = null//左边按钮文字
    private var showBtnLeft: Boolean = false
    private var showBtnRight: Boolean = false

    private var btnOk: Button? = null
    private var btnCancel: Button? = null

    private val mButtonHandler = View.OnClickListener { view ->
        if (view === btnCancel) {
            if (mDialog.get() == null) return@OnClickListener
            if (mNegativeButtonListener != null) {
                mNegativeButtonListener?.onClick(mDialog.get()!!)
            }
        } else if (view === btnOk) {
            if (mDialog.get() == null) return@OnClickListener
            if (mPositiveButtonListener != null) {
                mPositiveButtonListener?.onClick(mDialog.get()!!)
            }
        }
    }

    fun setChildView(view: View) {
        dialogView = view
        dealDefaultDialog(
            mPositiveButtonListener, mNegativeButtonListener, titleStr,
            contentStr, showBtnLeft, negativeStr, showBtnRight, positiveStr
        )
    }

    private fun dealDefaultDialog(
        positiveBtnListener: IDialog.OnClickListener?,
        negativeBtnListener: IDialog.OnClickListener?,
        titleStr: String?,
        contentStr: String?,
        showBtnLeft: Boolean,
        negativeStr: String?,
        showBtnRight: Boolean,
        positiveStr: String?
    ) {
        if (dialogView == null) return
        this.mNegativeButtonListener = negativeBtnListener
        this.mPositiveButtonListener = positiveBtnListener
        btnOk = dialogView?.findViewById<View>(R.id.btnOk) as Button
        btnCancel = dialogView?.findViewById<View>(R.id.btnCancel) as Button

        if (showBtnRight && showBtnLeft) {
            //左右两个按钮都存在
            if (btnOk != null) {
                btnOk?.visibility = View.VISIBLE
                btnOk?.text = Html.fromHtml(if (TextUtils.isEmpty(positiveStr)) "确定" else positiveStr)
                btnOk?.setOnClickListener(mButtonHandler)
            }
            if (btnCancel != null) {
                btnCancel?.visibility = View.VISIBLE
                btnCancel?.text = Html.fromHtml(if (TextUtils.isEmpty(negativeStr)) "取消" else negativeStr)
                btnCancel?.setOnClickListener(mButtonHandler)
            }
        } else if (showBtnRight) {
            //只有右边的按钮
            if (btnOk != null) {
                btnOk?.visibility = View.VISIBLE
                btnOk?.setBackgroundResource(R.drawable.lib_ui_selector_btn_border_bg)
                btnOk?.text = Html.fromHtml(if (TextUtils.isEmpty(positiveStr)) "确定" else positiveStr)
                btnOk?.setOnClickListener(mButtonHandler)
            }
        } else if (showBtnLeft) {
            //只有左边的按钮
            if (btnCancel != null) {
                btnCancel?.visibility = View.VISIBLE
                btnCancel?.setBackgroundResource(R.drawable.lib_ui_selector_btn_border_bg)
                btnCancel?.text = Html.fromHtml(if (TextUtils.isEmpty(negativeStr)) "取消" else negativeStr)
                btnCancel?.setOnClickListener(mButtonHandler)
            }
        }

        val tvTitle = dialogView?.findViewById<View>(R.id.dialogTitle) as? TextView
        val tvContent = dialogView?.findViewById<View>(R.id.dialogContent) as? TextView

        if (tvTitle != null) {
            tvTitle.visibility = if (TextUtils.isEmpty(titleStr)) View.GONE else View.VISIBLE
            tvTitle.text = Html.fromHtml(if (!TextUtils.isEmpty(titleStr)) titleStr else "Title")
            if (TextUtils.isEmpty(contentStr) && mDialog.get() != null && mDialog.get()?.mContext != null) {
                tvTitle.minHeight = ScreenUtil.dp2px(mDialog.get()?.mContext!!, 100f)
                tvTitle.gravity = Gravity.CENTER
                tvTitle.setPadding(0, 10, 0, 0)
            }
        }
        if (tvContent != null) {
            tvContent.visibility = if (TextUtils.isEmpty(contentStr)) View.GONE else View.VISIBLE
            tvContent.text = contentStr
            tvContent.viewTreeObserver.addOnPreDrawListener(ViewTreeObserver.OnPreDrawListener {
                val lineCount = tvContent.lineCount
                if (lineCount >= 3) {
                    //超过三行居左显示
                    tvContent.gravity = Gravity.START
                } else {
                    //默认居中
                    tvContent.gravity = Gravity.CENTER_HORIZONTAL
                    if (TextUtils.isEmpty(titleStr)) {
                        tvContent.setPadding(0, 50, 0, 50)
                    }
                }

                if (TextUtils.isEmpty(titleStr)) {
                    //没有title，只有content
                    tvContent.textSize = 18f
                    if (mDialog.get() == null || mDialog.get()?.mContext == null || mDialog.get()?.mContext?.resources == null)
                        return@OnPreDrawListener true
                    tvContent.setTextColor(mDialog.get()?.mContext?.resources?.getColor(R.color.c333333)!!)
                }
                true
            })

        }

    }

    class SYParams {
        internal var fragmentManager: FragmentManager? = null
        internal var layoutRes: Int = 0
        internal var dialogWidth: Int = 0
        internal var dialogHeight: Int = 0
        internal var dimAmount = 0.4f
        var gravity = Gravity.CENTER
        internal var isCancelableOutside = true
        internal var cancelable = false
        internal var dialogView: View? = null
        internal var context: Context? = null
        internal var positiveBtnListener: IDialog.OnClickListener? = null
        internal var negativeBtnListener: IDialog.OnClickListener? = null
        internal var titleStr: String? = null//默认标题
        internal var contentStr: String? = null//默认内容
        internal var positiveStr: String? = null//右边按钮文字
        internal var negativeStr: String? = null//左边按钮文字
        internal var showBtnLeft: Boolean = false
        internal var showBtnRight: Boolean = false
        internal var animRes = R.style.translate_style//Dialog动画style

        internal fun apply(controller: MyDialogController) {
            controller.dimAmount = dimAmount
            controller.gravity = gravity
            controller.isCancelableOutside = isCancelableOutside
            controller.isCancelable = cancelable
            controller.animRes = animRes
            controller.titleStr = titleStr
            controller.contentStr = contentStr
            controller.positiveStr = positiveStr
            controller.negativeStr = negativeStr
            controller.showBtnLeft = showBtnLeft
            controller.showBtnRight = showBtnRight
            controller.mPositiveButtonListener = positiveBtnListener
            controller.mNegativeButtonListener = negativeBtnListener
            when {
                layoutRes > 0 -> controller.layoutRes = layoutRes
                dialogView != null -> controller.dialogView = dialogView
                else -> throw IllegalArgumentException("Dialog View can't be null")
            }
            if (dialogWidth > 0) {
                controller.dialogWidth = dialogWidth
            }
            if (dialogHeight > 0) {
                controller.dialogHeight = dialogHeight
            }
        }

    }

}
